package com.adams.access.db.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 
 * 
 * File Name : ViewItem.java
 * 
 * @Description : 页面内容项实体
 */
@Entity
@Table(name = "T_VIEW_ITEM_INFO")
public class ViewItem extends EntityObject
{

	private static final long serialVersionUID = -7116831782618446809L;

	public static String XTYPE_1 = "dynamicCombo";

	public static String XTYPE_2 = "textfield";

	public static String XTYPE_3 = "datefield";

	public static String XTYPE_4 = "between_datefield";

	public static String XTYPE_5 = "fileuploadfield";

	public static String XTYPE_6 = "hidden";

	public static String XTYPE_7 = "compositeSelect";

	public static String XTYPE_8 = "dynamicRadioGroup";

	public static String XTYPE_9 = "advancedSearch_numberfield";

	public static String XTYPE_11 = "advancedSearch_datefield";

	public static String NAME_SEQ = ",";

	public static String VIEW_NAME_SEQ = "_";

	private String tablecode;// 表名

	private String name;// 字段项名
	private String nameNoEnumerateTag = "";// 不包含枚举标识的字段项名，是对原字段项名经过处理后获得

	private String value;// 默认值

	private String xtype;// 类型 dynamicCombo textfield datefield

	// private String format;// 格式化类型 用于时间输入
	public static String DATE_FORMAT = "Y-m-d";// 日期的输入格式
	public static String TIME_FORMAT = "Y-m-d H:i:s";// 时间的输入格式

	private String fieldLabel;// 字段标签名

	private String width;// 宽度

	private String url;// 下拉选项请求URL

	private String displayField;// 下拉选项显示的字段

	private String valueField;// 下拉选项选择值的字段

	private String iconClsField;// 下拉选项图标字段

	private String vtype;// 检验类型

	private String blankText;// 空的提示信息

	private String maxLengthText;// 最大长度提示

	private String minLengthText;// 最小长度提交

	private String regexpr;// 正则表达式

	private String regexprText;// 正则表达式检证失败提示

	private Boolean allowBlank;// 允许为空

	private Boolean hidden = Boolean.FALSE;// 是否隐藏

	private Boolean editable;// 下拉框是否允许输入

	private boolean iconCombo;// 是否显示下拉选项图标

	private boolean staticFlag;// 是否使用静态数据，否使用url请求，是直接在staticData属性中返回

	private boolean loadForce;// 是否需要预加载 提供给下拉选项

	private boolean readOnly = Boolean.FALSE;// 只读

	private Integer maxLength;// 最大长度

	private int datadecimals = 0;// 小数位

	private Integer minLength;// 最小长度

	private Integer minChars;// 下拉选项发送请求的最小值

	private boolean orderFlag;// 排序标识

	private String orderSc;// 排序方向

	public static String ORDER_SC_ASC = "1";// 升序
	public static String ORDER_SC_DESC = "0";// 倒序

	private String buttonText;// 按钮文字

	private Integer orderid = 0;// 页面上排列顺序

	private Long viewpanelid;// 界面Panelid

	private Long viewwinid;// 页面窗口主键

	@Column(name = "NAME")
	public String getName()
	{
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	@Column(name = "VALUE")
	public String getValue()
	{
		return value;
	}

	public void setValue(String value)
	{
		this.value = value;
	}

	@Column(name = "XTYPE")
	public String getXtype()
	{
		return xtype;
	}

	/**
	 * @param xtype
	 *            the xtype to set
	 */
	public void setXtype(String xtype)
	{
		this.xtype = xtype;
	}

	@Column(name = "FIELD_LABEL")
	public String getFieldLabel()
	{
		return fieldLabel;
	}

	/**
	 * @param fieldLabel
	 *            the fieldLabel to set
	 */
	public void setFieldLabel(String fieldLabel)
	{
		this.fieldLabel = fieldLabel;
	}

	@Column(name = "WIDTH")
	public String getWidth()
	{
		return width;
	}

	/**
	 * @param width
	 *            the width to set
	 */
	public void setWidth(String width)
	{
		this.width = width;
	}

	@Column(name = "URL")
	public String getUrl()
	{
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url)
	{
		this.url = url;
	}

	@Column(name = "DISPLAY_FIELD")
	public String getDisplayField()
	{
		return displayField;
	}

	/**
	 * @param displayField
	 *            the displayField to set
	 */
	public void setDisplayField(String displayField)
	{
		this.displayField = displayField;
	}

	@Column(name = "VALUE_FIELD")
	public String getValueField()
	{
		return valueField;
	}

	/**
	 * @param valueField
	 *            the valueField to set
	 */
	public void setValueField(String valueField)
	{
		this.valueField = valueField;
	}

	@Column(name = "ICON_CLS_FIELD")
	public String getIconClsField()
	{
		return iconClsField;
	}

	/**
	 * @param iconClsField
	 *            the iconClsField to set
	 */
	public void setIconClsField(String iconClsField)
	{
		this.iconClsField = iconClsField;
	}

	@Column(name = "VTYPE")
	public String getVtype()
	{
		return vtype;
	}

	/**
	 * @param vtype
	 *            the vtype to set
	 */
	public void setVtype(String vtype)
	{
		this.vtype = vtype;
	}

	@Column(name = "BLANK_TEXT")
	public String getBlankText()
	{
		return blankText;
	}

	/**
	 * @param blankText
	 *            the blankText to set
	 */
	public void setBlankText(String blankText)
	{
		this.blankText = blankText;
	}

	@Column(name = "MAX_LENGTH_TEXT")
	public String getMaxLengthText()
	{
		return maxLengthText;
	}

	/**
	 * @param maxLengthText
	 *            the maxLengthText to set
	 */
	public void setMaxLengthText(String maxLengthText)
	{
		this.maxLengthText = maxLengthText;
	}

	@Column(name = "MIN_LENGTH_TEXT")
	public String getMinLengthText()
	{
		return minLengthText;
	}

	/**
	 * @param minLengthText
	 *            the minLengthText to set
	 */
	public void setMinLengthText(String minLengthText)
	{
		this.minLengthText = minLengthText;
	}

	@Column(name = "REGEXPR")
	public String getRegexpr()
	{
		return regexpr;
	}

	/**
	 * @param regEx
	 *            the regEx to set
	 */
	public void setRegexpr(String regexpr)
	{
		this.regexpr = regexpr;
	}

	@Column(name = "REGEXPR_TEXT")
	public String getRegexprText()
	{
		return regexprText;
	}

	public void setRegexprText(String regexprText)
	{
		this.regexprText = regexprText;
	}

	/**
	 * Description : 正刚表达式名称
	 * 
	 * @return
	 * 
	 */
	@Transient
	public String getRegexprName()
	{
		return "ViewItem_Regexpr_Name_" + id;
	}

	@Column(name = "ALLOW_BLANK")
	public Boolean getAllowBlank()
	{
		return allowBlank;
	}

	/**
	 * @param allowBlank
	 *            the allowBlank to set
	 */
	public void setAllowBlank(Boolean allowBlank)
	{
		this.allowBlank = allowBlank;
	}

	@Column(name = "HIDDEN")
	public Boolean getHidden()
	{
		return hidden;
	}

	/**
	 * @param hidden
	 *            the hidden to set
	 */
	public void setHidden(Boolean hidden)
	{
		this.hidden = hidden;
	}

	@Column(name = "EDITABLE")
	public Boolean getEditable()
	{
		return editable;
	}

	/**
	 * @param editable
	 *            the editable to set
	 */
	public void setEditable(Boolean editable)
	{
		this.editable = editable;
	}

	@Column(name = "ICON_COMBO")
	public boolean isIconCombo()
	{
		return iconCombo;
	}

	/**
	 * @param iconCombo
	 *            the iconCombo to set
	 */
	public void setIconCombo(boolean iconCombo)
	{
		this.iconCombo = iconCombo;
	}

	@Column(name = "STATIC_FLAG")
	public boolean isStaticFlag()
	{
		return staticFlag;
	}

	/**
	 * @param staticFlag
	 *            the staticFlag to set
	 */
	public void setStaticFlag(boolean staticFlag)
	{
		this.staticFlag = staticFlag;
	}

	@Column(name = "LOAD_FORCE")
	public boolean isLoadForce()
	{
		return loadForce;
	}

	/**
	 * @param loadForce
	 *            the loadForce to set
	 */
	public void setLoadForce(boolean loadForce)
	{
		this.loadForce = loadForce;
	}

	@Column(name = "MAX_LENGTH")
	public Integer getMaxLength()
	{
		return maxLength;
	}

	/**
	 * @param maxLength
	 *            the maxLength to set
	 */
	public void setMaxLength(Integer maxLength)
	{
		this.maxLength = maxLength;
	}

	@Column(name = "MIN_LENGTH")
	public Integer getMinLength()
	{
		return minLength;
	}

	/**
	 * @param minLength
	 *            the minLength to set
	 */
	public void setMinLength(Integer minLength)
	{
		this.minLength = minLength;
	}

	@Column(name = "MIN_CHARS")
	public Integer getMinChars()
	{
		return minChars;
	}

	/**
	 * @param minChars
	 *            the minChars to set
	 */
	public void setMinChars(Integer minChars)
	{
		this.minChars = minChars;
	}

	@Column(name = "READ_ONLY")
	public boolean isReadOnly()
	{
		return readOnly;
	}

	public void setReadOnly(boolean readOnly)
	{
		this.readOnly = readOnly;
	}

	@Column(name = "TABLECODE")
	public String getTablecode()
	{
		return tablecode;
	}

	public void setTablecode(String tablecode)
	{
		this.tablecode = tablecode;
	}

	@Column(name = "ORDER_FLAG")
	public boolean getOrderFlag()
	{
		return orderFlag;
	}

	public void setOrderFlag(boolean orderFlag)
	{
		this.orderFlag = orderFlag;
	}

	@Column(name = "ORDER_SC")
	public String getOrderSc()
	{
		return orderSc;
	}

	public void setOrderSc(String orderSc)
	{
		this.orderSc = orderSc;
	}

	@Column(name = "BUTTON_TEXT")
	public String getButtonText()
	{
		return buttonText;
	}

	public void setButtonText(String buttonText)
	{
		this.buttonText = buttonText;
	}

	@Column(name = "ORDERID")
	public Integer getOrderid()
	{
		return orderid;
	}

	public void setOrderid(Integer orderid)
	{
		if (null != orderid)
		{
			this.orderid = orderid;
		}
	}

	@Column(name = "VIEWPANELID")
	public Long getViewpanelid()
	{
		return viewpanelid;
	}

	/**
	 * @param viewpanelid
	 *            the viewpanelid to set
	 */
	public void setViewpanelid(Long viewpanelid)
	{
		this.viewpanelid = viewpanelid;
	}

	@Column(name = "VIEWWINID")
	public Long getViewwinid()
	{
		return viewwinid;
	}

	public void setViewwinid(Long viewwinid)
	{
		this.viewwinid = viewwinid;
	}

	@Transient
	public String getFormat()
	{
		if (XTYPE_3.equals(xtype) || XTYPE_4.equals(xtype))
		{
			return DATE_FORMAT;
		} else
		{
			return "";
		}
		// return format;
	}

	/**
	 * Description : name属性是否是多级复合格式
	 * 
	 * @return
	 * 
	 */
	@Transient
	public boolean isMultilevelByName()
	{
		boolean result = false;
		if (name != null && !"".equals(name) && name.indexOf(NAME_SEQ) > -1)
		{
			result = true;
		}
		return result;
	}

	/**
	 * Description : 解析name属性，得到字段编码列表
	 * 
	 * @return
	 * 
	 */
	@Transient
	public List<String> getColumncodes()
	{
		List<String> result = new ArrayList<String>();
		if (null != name && !"".equals(name.trim()))
		{
			StringTokenizer st = new StringTokenizer(name, NAME_SEQ);
			while (st.hasMoreTokens())
			{
				String column = st.nextToken();
				if (null != column && !"".equals(column.trim()))
				{
					result.add(column);
				}
			}
		}
		return result;
	}

	/**
	 * Description : 解析name属性，得到首个字段编码
	 * 
	 * @return
	 * 
	 */
	@Transient
	public String getFirstColumncode()
	{
		List<String> columncodes = getColumncodes();
		if (null == columncodes || columncodes.size() == 0)
		{
			return null;
		}
		return columncodes.get(0);
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + id.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final ViewItem other = (ViewItem) obj;
		if (id != other.id)
			return false;
		return true;
	}

	@Transient
	public String getViewName()
	{
		if (isMultilevelByName())
		{
			return name.replaceAll(NAME_SEQ, VIEW_NAME_SEQ);
		}
		return name;
	}

	@Transient
	public int getDatadecimals()
	{
		return datadecimals;
	}

	public void setDatadecimals(int datadecimals)
	{
		this.datadecimals = datadecimals;
	}

	@Transient
	public String getNameNoEnumerateTag()
	{
		return nameNoEnumerateTag;
	}

	@Transient
	public String getViewNameNoEnumerateTag()
	{
		if (isMultilevelByName() && nameNoEnumerateTag.indexOf(NAME_SEQ) > -1)
		{
			return nameNoEnumerateTag.replaceAll(NAME_SEQ, VIEW_NAME_SEQ);
		}
		return nameNoEnumerateTag;
	}

	public void setNameNoEnumerateTag(String nameNoEnumerateTag)
	{
		this.nameNoEnumerateTag = nameNoEnumerateTag;
	}

}
